package com.boyaa.shells.tool 
{
	/**
	 * ...
	 * @author Jacklee
	 */ 
	import com.boyaa.shells.data.PlayerDataList;
	import flash.events.NetStatusEvent;
    import flash.net.SharedObject;    
  
    public class Cookie 
    {    
		private var _name:String;  
		private var _so:SharedObject;  
		
		public function Cookie(name:String = "klstudio") 
		{  
			//加ID是避免不同用户相同任务是否是新任务的判断产生影响的问题。
			if (name == "Mission") {
				_name = name + "_" + PlayerDataList.instance.selfData.uid;
			}else {
				_name = name;
			}
			_so = SharedObject.getLocal(_name, "/");  
		}   
			
		//获取名称;  
		public function getName():String 
		{  
			return _name;  
		}  
			
		//清除Cookie所有值;  
		public function clear():void 
		{  
			_so.clear();  
		}  
			
		//添加Cookie值  
		public function put(key:String, value:*):void 
		{  
			key = "key_"+key;  
			if(_so.data.cookie == undefined){  
				var obj:Object = {};  
				obj[key] = value;  
				_so.data.cookie = obj;  
			}else{  
				_so.data.cookie[key] = value;  
			}  
			 _so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
		}  
		
		private function onFlushStatus(event:NetStatusEvent):void
		{
            switch (event.info.code) 
			{
                case "SharedObject.Flush.Success":
					_so.flush(); 
                    break;
                case "SharedObject.Flush.Failed":
                    new Tiptext("添加Cookie值,無法寫入數據!");
                    break;
            }
		}
		
		//删除Cookie值;  
		public function remove(key:String):void 
		{  
			if (contains(key)) {  
				delete _so.data.cookie["key_" + key];  
				_so.flush();  
			}  
		}  
			
		//获取Cookie值;  
		public function getObj(key:String):Object
		{       
			return contains(key)?_so.data.cookie["key_" + key]:null;
		}  
			
		//Cookie值是否存在;  
		public function contains(key:String):Boolean
		{  
			key = "key_" + key;   
			return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;  
		}  
		
		public function get so():SharedObject 
		{
			return _so;
		}
	}
}